#
# Copyright (c) 2022. China Mobile (SuZhou) Software Technology Co.,Ltd. All rights reserved.
# Lakehouse is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#          http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
#

# Usage:
# $ mvn clean install -DskipTests \
#       -Ddocker.hub=${your_docker_hub} \
#       -Ddocker.repo=${your_docker_repo} \
#       -Ddocker.hub.username=${your_docker_hub_username} \
#       -Ddocker.hub.password=${your_docker_hub_password} \
#       -pl lakehouse-engine/engine-spark-sql -am dockerfile:build dockerfile:push

FROM openjdk:8-jre-slim

ARG JAR_FILE
ARG spark_uid=185

RUN set -ex && \
    sed -i 's/http:\/\/deb.\(.*\)/https:\/\/deb.\1/g' /etc/apt/sources.list && \
    apt-get update && \
    ln -s /lib /lib64 && \
    apt install -y bash tini libc6 libpam-modules krb5-user libnss3 procps curl && \
    mkdir -p /opt/spark && \
    mkdir -p /opt/spark/work-dir && \
    touch /opt/spark/RELEASE && \
    rm /bin/sh && \
    ln -sv /bin/bash /bin/sh && \
    echo "auth required pam_wheel.so use_uid" >> /etc/pam.d/su && \
    chgrp root /etc/passwd && chmod ug+rw /etc/passwd && \
    rm -rf /var/cache/apt/* && \
    curl -O -L https://mirrors.bfsu.edu.cn/apache/spark/spark-3.1.2/spark-3.1.2-bin-hadoop3.2.tgz && \
    tar zxf spark-3.1.2-bin-hadoop3.2.tgz && \
    cd spark-3.1.2-bin-hadoop3.2/ && \
    cp -ar jars /opt/spark && \
    cp -ar bin /opt/spark && \
    cp -ar sbin /opt/spark && \
    cp kubernetes/dockerfiles/spark/entrypoint.sh /opt && \
    cp kubernetes/dockerfiles/spark/decom.sh /opt && \
    cp -ar examples /opt/spark && \
    cp -ar kubernetes/tests /opt/spark && \
    cp -ar data /opt/spark && \
    curl -O -L https://repo.maven.apache.org/maven2/com/amazonaws/aws-java-sdk-bundle/1.11.271/aws-java-sdk-bundle-1.11.271.jar && \
    mv aws-java-sdk-bundle-1.11.271.jar /opt/spark/jars && \
    curl -O -L https://repo.maven.apache.org/maven2/org/apache/hadoop/hadoop-aws/3.1.0/hadoop-aws-3.1.0.jar && \
    mv hadoop-aws-3.1.0.jar /opt/spark/jars && \
    cd .. && rm -rf spark-3.1.2-bin-hadoop3.2/ spark-3.1.2-bin-hadoop3.2.tgz

COPY ${JAR_FILE} /opt/spark/examples/jars

ENV SPARK_HOME /opt/spark

WORKDIR /opt/spark/work-dir
RUN chmod g+w /opt/spark/work-dir
RUN chmod a+x /opt/decom.sh

ENTRYPOINT [ "/opt/entrypoint.sh" ]

# Specify the User that the actual main process will run as
USER ${spark_uid}